/**
 * Created by chenlin on 2017/12/20.
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/dutyManager/dutyManager.js',
  './custom/widgets/js/YufpUserSelector.js',
  './custom/widgets/js/yufpOrgTree.js',
  './custom/widgets/js/yufpExtTree.js'
],
function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.lookup.reg('DATA_STS');
    var belongOrgId = '';

    yufp.custom.vue({
      el: '#dutyManager',
      data: function () {
        var me = this;
        return {
          formdata: {},
          dataRoot: { orgId: yufp.session.org.id, orgName: yufp.session.org.name },
          orgTreeUrl: backend.appOcaService + '/api/util/getorg',
          dataParams: { orgSts: 'A', lazy: true },
          dutyTableUrl: backend.appOcaService + '/api/adminsmduty/list',
          dutyTableParams: {
            condition: JSON.stringify({
              belongOrgId: yufp.session.org.id
            })
          },
          dutyUserUrl: backend.appOcaService + '/api/adminsmduty/userlist',
          addButton: !yufp.session.checkCtrl('add'), // 新增按钮控制
          modifyButton: !yufp.session.checkCtrl('modify'), // 修改按钮控制
          deleteButton: !yufp.session.checkCtrl('delete'), // 删除按钮控制
          enableButton: !yufp.session.checkCtrl('enable'), // 启用按钮控制
          disableButton: !yufp.session.checkCtrl('disable'), // 停用按钮控制
          dutyUserButton: !yufp.session.checkCtrl('dutyUser'), // 岗位用户按钮控制
          expandCollapseName: ['base'],
          tableColumns: [
            { label: this.$t('dutyManager.ssjg'), prop: 'orgName' },
            { label: this.$t('dutyManager.gwdm'), prop: 'dutyCde' },
            {
              label: this.$t('dutyManager.gwmc'),
              prop: 'dutyName',
              sortable: true,
              resizable: true,
              template: function () {
                return '<template scope="scope">\
                                <a onclick="return false;" href="javascipt:void(0);" style="text-decoration:underline;" @click="_$event(\'custom-detail-click\', scope)">{{ scope.row.dutyName }}</a>\
                            </template>';
              }
            },
            { label: this.$t('dutyManager.zt'), prop: 'dutySts', width: 80, dataCode: 'DATA_STS' },
            { label: this.$t('dutyManager.zxgxr'), prop: 'userName' },
            {
              label: this.$t('dutyManager.zxbgsj'),
              prop: 'lastChgDt',
              formatter: function (row, me) {
                return row.lastChgDt.substring(0, 10);
              }
            }
          ],
          updateFields: [{
            columnCount: 1,
            fields: [
              {
                field: 'dutyCde',
                label: this.$t('dutyManager.gwdm'),
                rules: [
                  { required: true, message: this.$t('dutyManager.btx'), trigger: 'blur' },
                  { max: 33, message: this.$t('dutyManager.srzgc'), trigger: 'blur' }]
              },
              {
                field: 'dutyName',
                label: this.$t('dutyManager.gwmc'),
                rules: [
                  { required: true, message: this.$t('dutyManager.btx'), trigger: 'blur' },
                  { max: 33, message: this.$t('dutyManager.srzgc'), trigger: 'blur' }]
              },
              {
                label: this.$t('dutyManager.ssjg'),
                field: 'belongOrgId',
                type: 'custom',
                is: 'yufp-org-tree',
                params: {
                  lazy: true,
                  needCheckbox: false,
                  dataRoot: { orgId: yufp.session.org.id, orgName: yufp.session.org.name },
                  dataParams: {
                    needDpt: true,
                    orgSts: 'A',
                    lazy: true
                  }
                },
                rules: [{ required: true, message: this.$t('dutyManager.btx'), trigger: 'blur' }]
              },
              {
                field: 'dutySts',
                label: this.$t('dutyManager.zt'),
                type: 'select',
                dataCode: 'DATA_STS',
                rules: [{ required: true, message: this.$t('dutyManager.btx'), trigger: 'blur' }]
              },
              { field: 'userName', label: this.$t('dutyManager.zxgxr') },
              { field: 'lastChgDt', label: this.$t('dutyManager.zxgxsj') }

            ]
          }],
          updateButtons: [
            {
              label: this.$t('dutyManager.bc'),
              type: 'primary',
              op: 'submit',
              hidden: false,
              click: function (model) {
                me.saveCreateFn();
              }
            },
            {
              label: this.$t('dutyManager.bc'),
              type: 'primary',
              op: 'submit',
              hidden: false,
              click: function (model) {
                me.saveEditFn();
              }
            },
            {
              label: this.$t('dutyManager.qx'),
              hidden: false,
              click: function (model) {
                me.dialogVisible = false;
              }
            }],
          height: yufp.custom.viewSize().height - 10,
          dialogVisible: false,
          formDisabled: false,
          viewType: 'DETAIL',
          viewTitle: yufp.lookup.find('CRUD_TYPE', false),
          // 岗位用户
          dialogGridVisible: false, // 编辑列表弹窗
          textMap: {
            dutyUser: this.$t('dutyManager.gwyh')
          },
          userQueryFields: [
            { placeholder: this.$t('dutyManager.dldm'), field: 'loginCode', type: 'input' },
            { placeholder: this.$t('dutyManager.ygxm'), field: 'userName', type: 'input' },
            { placeholder: this.$t('dutyManager.ygh'), field: 'userCode', type: 'input' }
          ],
          userQueryButtons: [
            {
              label: '查询',
              op: 'submit',
              type: 'primary',
              click: function (model, valid) {
                if (valid) {
                  var param = {
                    condition: JSON.stringify({
                      loginCode: me.$refs.userQuery.fm.loginCode ? me.$refs.userQuery.fm.loginCode : null,
                      userName: me.$refs.userQuery.fm.userName ? me.$refs.userQuery.fm.userName : null,
                      userCode: me.$refs.userQuery.fm.userCode ? me.$refs.userQuery.fm.userCode : null,
                      userSts: me.$refs.userQuery.fm.userSts ? me.$refs.userQuery.fm.userSts : null,
                      dutyId: me.$refs.dutyTable.selections[0].dutyId
                    })
                  };
                  me.$refs.userTable.remoteData(param);
                }
              }
            },
            {
              label: '重置',
              op: 'reset',
              click: function () { }
            }
          ],
          userTableColumns: [
            { label: this.$t('dutyManager.dldm'), prop: 'loginCode', width: 180 },
            { label: this.$t('dutyManager.ygxm'), prop: 'userName', sortable: true, resizable: true },
            { label: this.$t('dutyManager.ssjg'), prop: 'orgName', width: 180 }
          ]
        };
      },
      methods: {
        // 主列表维护
        queryMainGridFn: function () {
          var me = this;
          var param = {
            condition: JSON.stringify({
              dutyCde: me.formdata.dutyCde ? me.formdata.dutyCde : null,
              dutyName: me.formdata.dutyName ? me.formdata.dutyName : null,
              dutySts: me.formdata.dutySts ? me.formdata.dutySts : null,
              belongOrgId: me.formdata.belongOrgId
            })
          };
          me.$refs.dutyTable.remoteData(param);
        },
        switchStatus: function (viewType, editable) {
          this.viewType = viewType;
          this.dialogVisible = true;
          this.formDisabled = !editable;
          this.updateButtons[2].hidden = !editable;
          if (viewType == 'ADD') {
            this.updateButtons[0].hidden = !editable;
            this.updateButtons[1].hidden = editable;
          } else if (viewType == 'EDIT') {
            this.updateButtons[0].hidden = editable;
            this.updateButtons[1].hidden = !editable;
          } else if (viewType == 'DETAIL') {
            this.updateButtons[0].hidden = !editable;
            this.updateButtons[1].hidden = !editable;
          }
        },
        nodeClickFn: function (nodeData, node, self) {
          this.formdata.belongOrgId = nodeData.orgId;
          belongOrgId = nodeData.orgId;
          this.queryMainGridFn();
        },
        addFn: function () {
          this.switchStatus('ADD', true);
          this.$nextTick(function () {
            this.$refs.dutyForm.resetFields();
            this.$refs.dutyForm.formModel.dutySts = 'A';
            this.$refs.dutyForm.formModel.belongOrgId = belongOrgId;
            // 设置编号可编辑
            this.updateFields[0].fields[0].readonly = false;
            // 设置字段隐藏
            this.updateFields[0].fields[4].hidden = true;
            this.updateFields[0].fields[5].hidden = true;
          });
        },
        modifyFn: function () {
          if (this.$refs.dutyTable.selections.length != 1) {
            this.$message({ message: this.$t('dutyManager.qxzytjl'), type: 'warning' });
            return;
          }
          if (this.$refs.dutyTable.selections[0].dutySts == 'A') {
            this.$message({ message: this.$t('dutyManager.znxzsxhdsxdsj'), type: 'warning' });
            return;
          }
          this.switchStatus('EDIT', true);
          this.$nextTick(function () {
            yufp.extend(this.$refs.dutyForm.formModel, this.$refs.dutyTable.selections[0]);
            // 设置编号可编辑
            this.updateFields[0].fields[0].readonly = true;
            // 设置字段隐藏
            this.updateFields[0].fields[4].hidden = true;
            this.updateFields[0].fields[5].hidden = true;
          });
        },
        infoFn: function (row) {
          // 设置字段显示
          this.updateFields[0].fields[4].hidden = false;
          this.updateFields[0].fields[5].hidden = false;
          this.switchStatus('DETAIL', false);
          this.$nextTick(function () {
            yufp.extend(this.$refs.dutyForm.formModel, row.row);
          });
        },
        saveCreateFn: function () {
          var fields = this.$refs.dutyForm;
          var vue = this;
          delete fields.formModel.dutyId;
          fields.formModel.lastChgUsr = yufp.session.userId;// 最新更新人
          this.$refs.dutyForm.validate(function (valid) {
            if (valid) {
              yufp.service.request({
                method: 'GET',
                url: backend.appOcaService + '/api/adminsmduty/checkdutycde',
                data: {
                  dutyCde: fields.formModel.dutyCde
                },
                callback: function (code, message, response) {
                  if (response > 0) {
                    vue.$message({ message: vue.$t('dutyManager.gwdmcf'), type: 'warning' });
                  } else {
                    yufp.service.request({
                      method: 'POST',
                      url: backend.appOcaService + '/api/adminsmduty/',
                      data: fields.formModel,
                      callback: function (code, message, response) {
                        vue.dialogVisible = false;
                        vue.$message({ message: vue.$t('dutyManager.sjbccg') });
                        vue.queryMainGridFn();
                      }
                    });
                  }
                }
              });
            }
          });
        },
        saveEditFn: function () {
          var fields = this.$refs.dutyForm;
          var vue = this;
          fields.formModel.lastChgUsr = yufp.session.userId;// 最新更新人
          this.$refs.dutyForm.validate(function (valid) {
            if (valid) {
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmduty/update',
                data: fields.formModel,
                callback: function (code, message, response) {
                  vue.dialogVisible = false;
                  vue.$message({ message: vue.$t('dutyManager.sjbccg') });
                  vue.queryMainGridFn();
                }
              });
            }
          });
        },
        useFn: function () {
          var vue = this;
          if (this.$refs.dutyTable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.dutyTable.selections.length; i++) {
              var row = this.$refs.dutyTable.selections[i];
              if (row.dutySts === 'W' || row.dutySts === 'I') {
                id = id + ',' + row.dutyId;
              } else {
                vue.$message({ message: vue.$t('dutyManager.znxzsxhdsxdsj'), type: 'warning' });
                return;
              }
            }
            this.$confirm(this.$t('dutyManager.cczjsxgwsxsfjx'), this.$t('dutyManager.ts'), {
              confirmButtonText: this.$t('dutyManager.qd'),
              cancelButtonText: this.$t('dutyManager.qx'),
              type: 'warning',
              center: true
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmduty/usebatch',
                data: {
                  id: id,
                  userId: yufp.session.userId
                },
                callback: function (code, message, response) {
                  vue.$message({ message: vue.$t('dutyManager.sjczcg') });
                  vue.queryMainGridFn();
                }
              });
            });
          } else {
            vue.$message({ message: vue.$t('dutyManager.qxxzysxdsj'), type: 'warning' });
            return;
          }
        },
        unUseFn: function () { // 批量失效
          var vue = this;
          if (this.$refs.dutyTable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.dutyTable.selections.length; i++) {
              var row = this.$refs.dutyTable.selections[i];
              if (row.dutySts === 'A') {
                id = id + ',' + row.dutyId;
              } else {
                this.$message({ message: this.$t('dutyManager.znxzsxdsj'), type: 'warning' });
                return;
              }
            }
            this.$confirm(this.$t('dutyManager.cczjsxgwsxsfjx'), this.$t('dutyManager.ts'), {
              confirmButtonText: this.$t('dutyManager.qd'),
              cancelButtonText: this.$t('dutyManager.qx'),
              type: 'warning',
              center: true
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmduty/unusebatch',
                data: {
                  id: id,
                  userId: yufp.session.userId
                },
                callback: function (code, message, response) {
                  if (code == 0) {
                    vue.$message({ message: response.message });
                    vue.queryMainGridFn();
                  }
                }
              });
            });
          } else {
            vue.$message({ message: vue.$t('dutyManager.qxxzysxdsj'), type: 'warning' });
            return;
          }
        },
        deleteFn: function () {
          var vue = this;
          if (this.$refs.dutyTable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.dutyTable.selections.length; i++) {
              var row = this.$refs.dutyTable.selections[i];
              if (row.dutySts != 'A') {
                id = id + ',' + row.dutyId;
              } else {
                vue.$message({ message: vue.$t('dutyManager.znscdsxhsxdsj'), type: 'warning' });
                return;
              }
            }
            yufp.service.request({
              method: 'GET',
              url: backend.appOcaService + '/api/adminsmduty/checkhasuser',
              data: {
                dutyId: id
              },
              callback: function (code, message, response) {
                if (response > 0) {
                  vue.$message({ message: vue.$t('dutyManager.sxzdgwxczyh'), type: 'warning' });
                } else {
                  vue.$confirm(vue.$t('dutyManager.cczjyjscgwjsfjx'), vue.$t('dutyManager.ts'), {
                    confirmButtonText: vue.$t('dutyManager.qd'),
                    cancelButtonText: vue.$t('dutyManager.qx'),
                    type: 'warning',
                    center: true
                  }).then(function () {
                    yufp.service.request({
                      method: 'POST',
                      url: backend.appOcaService + '/api/adminsmduty/deletebatch',
                      data: {
                        id: id
                      },
                      callback: function (code, message, response) {
                        vue.$message({ message: vue.$t('dutyManager.sjsccg') });
                        vue.queryMainGridFn();
                      }
                    });
                  });
                }
              }
            });
          } else {
            vue.$message({ message: vue.$t('dutyManager.qxxzyscdsj'), type: 'warning' });
            return;
          }
        },
        // 岗位用户列表维护
        openDutyUserFn: function () {
          if (this.$refs.dutyTable.selections.length != 1) {
            this.$message({ message: this.$t('dutyManager.qxxzytjl'), type: 'warning' });
            return;
          }
          this.dialogDisabled = false;
          this.dialogStatus = 'dutyUser';
          this.dialogGridVisible = true;
          this.queryUserGridFn();
        },
        queryUserGridFn: function () {
          var me = this;
          this.$nextTick(function () {
            var param = {
              condition: JSON.stringify({
                loginCode: me.$refs.userQuery.fm.loginCode ? me.$refs.userQuery.fm.loginCode : null,
                userName: me.$refs.userQuery.fm.userName ? me.$refs.userQuery.fm.userName : null,
                userCode: me.$refs.userQuery.fm.userCode ? me.$refs.userQuery.fm.userCode : null,
                userSts: me.$refs.userQuery.fm.userSts ? me.$refs.userQuery.fm.userSts : null,
                dutyId: me.$refs.dutyTable.selections[0].dutyId
              })
            };
            me.$refs.userTable.remoteData(param);
          });
        }
      }
    });
  };
});
